﻿using Beryl.Model;
using Beryl.Util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace Beryl.OrderChanger
{
    //订单转单查找账号类
    public class userController
    {
        private static userController instance = null;
        private static readonly object uniqueLock = new object();

        public static userController Instance
        {
            get
            {
                lock (uniqueLock)
                {
                    if (instance == null)
                    {
                        instance = new userController();
                    }
                }
                return instance;
            }
        }
        public userController() { }

        public List<string> getUser(string para)
        {
            Newtonsoft.Json.Linq.JArray jArray = CommonMethod.Instance.jsonToJsonArray(para);
            List<string> list = new List<string>();
            string sql;
            try
            {
                if(jArray != null)
                {
                    var k = jArray[0];
                    sql = string.Format(@"select * from [dbo].[Account] where UserName = '{0}';", k["账号"].ToString());
                    var userData = CommonMethod.Instance.selectDatabase(sql);
                    var userDatas = userData.Select($"UserName = '{k["账号"].ToString()}'");
                    if(userDatas.Length > 0)
                    {
                        var userDataRows = userDatas[0];
                        string password = k["密码"].ToString();
                        if (userDataRows["passWord"].ToString().Equals(password))
                        {
                            int id = int.Parse(userDataRows["id"].ToString());
                            string time = CommonMethod.Instance.getTimes();
                            string upDateAccount = string.Format(@"update [dbo].[Account] set LoginTime = '{0}' where id = {1};", time,id);
                            var connstr = SqlHelper.GetConnSting();
                            SqlHelper.ExecuteNonQuery(connstr, CommandType.Text, upDateAccount);
                            string json = JsonConvert.SerializeObject(userData);
                            list.Add(json);
                        }else
                        {
                            throw new Exception("密码错误");
                        }
                    }else
                    {
                        throw new Exception("查找用户失败");
                    }
                }else
                {
                    throw new Exception("转换Json失败");
                }
            }
            catch(Exception e)
            {
                list.Add(e.Message);
            }
            return list;
        }

        //查找账号信息
        public List<string> UserQuery(string para)
        {
            List<string> list = new List<string>();
            try
            {
                string sql = "SELECT [id],[UserName],[passWord],[workShop],[ProductDepNo],[Role],[DepartMent],[LoginTime] FROM Account";
                string strPicYZ = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(sql));
                if(strPicYZ.Equals("[]"))
                {
                    throw new Exception("查询失败");
                }
                list.Add(strPicYZ);
            }
            catch(Exception e)
            {
                list.Add(e.Message);
            }
            return list;
        }

        public List<string> UserAdd(string para)
        {
            Newtonsoft.Json.Linq.JArray jArray = CommonMethod.Instance.jsonToJsonArray(para);
            List<string> list = new List<string>();
            try
            {
                var k = jArray[0];
                string UserName = k["用户名"].ToString();
                string password = k["密码"].ToString();
                string workshop = k["账号角色"].ToString();
                string productDepNo = k["部门代码"].ToString();
                string Role = k["角色"].ToString();
                string deparment = k["部门"].ToString();
                string createTime = CommonMethod.Instance.getTimes();

                string sql = string.Format(@"insert into [dbo].[Account] (UserName,passWord,workShop,ProductDepNo,Role,DepartMent,LoginTime) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}');",
                    UserName,password,workshop,productDepNo,Role,deparment,createTime);
                var connstr = SqlHelper.GetConnSting();
                SqlHelper.ExecuteNonQuery(connstr, CommandType.Text, sql);
                list.Add("success");
            }
            catch(Exception e)
            {
                list.Add(e.Message);
            }
            return list;
        }
    }
}